// ^2_3^
/// NOTE:如果responseBody放外面，在builder时，莫名串数据；(todo)
/// 响应体
// const responseBody = {
//   message: '',
//   timestamp: 0,
//   result: null,
//   code: 0
// }
//
/// 创建响应
// export const builder = (data, message, code = 0, headers = {}) => {
//   responseBody.result = data
//   console.log('data >', data)

//   if (message !== undefined && message !== null) {
//     responseBody.message = message
//   }

//   if (code !== undefined && code !== 0) {
//     responseBody.code = code
//     responseBody._status = code
//   }
//   if (headers !== null && typeof headers === 'object' && Object.keys(headers).length > 0) {
//     responseBody._headers = headers
//   }

//   responseBody.timestamp = new Date().getTime()

//   console.log('responseBody >', responseBody)
//   return responseBody
// }
export const builder = (data, message, code = 0, headers = {}) => {
  const responseBody = {
    message: '',
    timestamp: 0,
    result: null,
    code: 0
  }

  responseBody.result = data
  // console.log('data >', data)

  if (message !== undefined && message !== null) {
    responseBody.message = message
  }

  if (code !== undefined && code !== 0) {
    responseBody.code = code
    responseBody._status = code
  }
  if (headers !== null && typeof headers === 'object' && Object.keys(headers).length > 0) {
    responseBody._headers = headers
  }

  responseBody.timestamp = new Date().getTime()

  // console.log('responseBody >', responseBody)
  return responseBody
}

// ^2_3^获取查询参数
export const getQueryParameters = (options) => {
  const url = options.url
  const search = url.split('?')[1]
  if (!search) {
    return {}
  }
  return JSON.parse('{"' + decodeURIComponent(search)
    .replace(/"/g, '\\"')
    .replace(/&/g, '","')
    .replace(/=/g, '":"') + '"}')
}

// 获取内容(解析)
export const getBody = (options) => {
  return options.body && JSON.parse(options.body)
}
